﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Nova_DAL;
using Nova_Common;
using System.Data.Common;
using System.Data;
//xgw 组盘解盘进行修改，去除了与入库明细表中的实际数量、组盘关系；只是组盘物料来源于入库单，组盘数量小于入库单计划数量；
      
public partial class Manage_Package : System.Web.UI.Page
{
    DbHelper dbHelper = new DbHelper();
    DbCommand cmd = null;
    DataSet ds = null;
    string strSQL = "";
    int iResNo = 0;
    string strResMsg = "";
    
    
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
        }
    }

    protected void gv_Bind()
    {
        HttpCookie WType = Request.Cookies["WType"];

        strSQL = @"SELECT TAS.PID,TAS.STOCKNO,TAS.MAT_CODE,TBM.MAT_NAME,TAS.NUM,TAS.SEC_MAT_ID,TBM.S01 FROM dbo.TAddStorage_List AS TAS 
                            LEFT JOIN TBase_Interface TBI ON TAS.Sec_Mat_ID=TBI.Sec_Mat_ID
                            LEFT JOIN TBase_Material TBM ON TAS.MAT_CODE=TBM.MAT_CODE 
                            WHERE TAS.StockNo='" + txt_StorageNo.Text + "' AND (TBI.S01 = '" + WType.Value.ToString().Trim() + "' OR 'FSSC'='" + WType.Value.ToString().Trim() + "')";
        cmd = dbHelper.GetSqlStringCommond(strSQL);
        gv.DataSource = dbHelper.ExecuteDataSet(cmd);
        gv.DataBind();
    }

    protected void ibtn_Search_Click(object sender, ImageClickEventArgs e)
    {
        gv_Bind();
        lab_Msg.Text = "";
    }

    protected void lbtn_ZP_Click(object sender, EventArgs e)
    {
        LinkButton lbtnZP = (LinkButton)sender;
        string[] sArray = lbtnZP.CommandArgument.ToString().Split('|');
        txt_Mat_Code.Text = sArray[0].ToString();
        HD_SEC_MAT_ID.Value = sArray[1].ToString();
        HD_Mat_Num.Value = sArray[2].ToString();
        
    }

    /// <summary>
    /// 重置
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    protected void ibtn_Reset_Click(object sender, ImageClickEventArgs e)
    {
        txt_Tray_ID.Text = "";
        txt_Mat_Code.Text = "";
        txt_Mat_Num.Text = "";
        HD_SEC_MAT_ID.Value = "";
        lab_Msg.Text = "";
    }

    /// <summary>
    /// 组盘
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    protected void ibtn_ZP_Click(object sender, ImageClickEventArgs e)
    {
        string strTray_ID = "";
        //if (HD_Mat_Num.Value == "")
        //{
        //    lab_Msg.Text = "请从上方列表中选择物料！";
        //    return;
        //}

        if (HD_SEC_MAT_ID.Value == "")
        {
            lab_Msg.Text = "请从上方列表中选择物料！";
            return;
        }
        if (Convert.ToDecimal(HD_Mat_Num.Value) < Convert.ToDecimal(txt_Mat_Num.Text.Trim()))
        {
            lab_Msg.Text = "组盘数量不允许超过计划数量！";
            return;
        }
        try
        {
            //if (rbtnl_Zone.SelectedValue == "1")
            //{

                if (txt_Tray_ID.Text == "")
                {
                    lab_Msg.Text = "托盘号不能为空！";
                    return;
                }

                strSQL = @"SELECT TOP 1 Structure FROM TRelations TRP 
			                    LEFT JOIN TBase_Position TBP ON TRP.Pos_ID = TBP.Pos_ID 
			                    LEFT JOIN TBase_Zone TBZ ON TBZ.Zone_ID = TBP.Zone_ID 
			                    WHERE TRP.TRAY_ID = @TRAY_ID";
                cmd = dbHelper.GetSqlStringCommond(strSQL);
                dbHelper.AddInParameter(cmd, "@TRAY_ID", DbType.String, txt_Tray_ID.Text.Trim());
                DataSet ds = dbHelper.ExecuteDataSet(cmd);
                if (ds != null && ds.Tables[0].Rows.Count > 0)
                {
                    strSQL = ds.Tables[0].Rows[0][0].ToString();
                    if (strSQL != "0")
                    {
                        lab_Msg.Text = "该托盘已经在立体库，不允许再次组盘！";
                        return;
                    }
                }
                strTray_ID = txt_Tray_ID.Text.ToString();
            //}
            //else
            //{
            //    strTray_ID = "H" + ddl_Zone.SelectedValue.ToString() + ddl_Dou.SelectedValue.ToString();               
            //}
            cmd = dbHelper.GetStoredProcCommond("SP_PACKAGE_ALL_100X");
            dbHelper.AddInParameter(cmd, "@TRAY_ID", DbType.String, strTray_ID);
            dbHelper.AddInParameter(cmd, "@SEC_MAT_ID", DbType.Int64, HD_SEC_MAT_ID.Value);
            dbHelper.AddInParameter(cmd, "@MAT_NUM", DbType.Int64, txt_Mat_Num.Text);
            dbHelper.AddInParameter(cmd, "@USERNAME", DbType.String, Request.Cookies["vNovaUserNo"].Value.ToString());
            dbHelper.AddOutParameter(cmd, "@ResNO", DbType.Int32, 100);
            dbHelper.AddOutParameter(cmd, "@RESMsg", DbType.String, 255);
            dbHelper.ExecuteNonQuery(cmd);
            iResNo = Convert.ToInt32(dbHelper.GetParameter(cmd, "@RESNO").Value.ToString());
            strResMsg = dbHelper.GetParameter(cmd, "@RESMsg").Value.ToString();

            if (iResNo > 0)
            {
                //谢刚伟注销组盘成功后 入库单明细表实际数量的变更；入库单明细表中TS01字段（组盘关系）的修改
                //strSQL = @"UPDATE TAddStorage_List SET ACT_NUM=ACT_NUM +'" + txt_Mat_Num.Text.Trim() + "' WHERE STOCKNO=@STOCKNO AND SEC_MAT_ID=@SEC_MAT_ID";
                //cmd = dbHelper.GetSqlStringCommond(strSQL);
                //dbHelper.AddInParameter(cmd, "@STOCKNO", DbType.String, txt_StorageNo.Text.Trim());
                //dbHelper.AddInParameter(cmd, "@SEC_MAT_ID", DbType.String, HD_SEC_MAT_ID.Value);
                //dbHelper.ExecuteNonQuery(cmd);
                
//                strSQL = @"UPDATE TAddStorage_List SET TS01=(
//                        SELECT TBS.PID FROM TBase_Storage TBS 
//	                    INNER JOIN TRel_TrayPosContainer TRT ON TRT.CONTAINERID=TBS.CONTAINERID
//	                    INNER JOIN TBase_TrayPos TBT ON TBT.RELATIONS_ID=TRT.RELATIONS_ID WHERE TBT.TRAY_ID=@TRAY_ID AND TBS.SEC_MAT_ID=@SEC_MAT_ID)
//                        WHERE STOCKNO=@STOCKNO AND SEC_MAT_ID=@SMI";
//                cmd = dbHelper.GetSqlStringCommond(strSQL);
//                dbHelper.AddInParameter(cmd, "@TRAY_ID", DbType.String, strTray_ID);
//                dbHelper.AddInParameter(cmd, "@SEC_MAT_ID", DbType.String, HD_SEC_MAT_ID.Value);
//                dbHelper.AddInParameter(cmd, "@STOCKNO", DbType.String, txt_StorageNo.Text.Trim());
//                dbHelper.AddInParameter(cmd, "@SMI", DbType.String, HD_SEC_MAT_ID.Value);
//                dbHelper.ExecuteNonQuery(cmd);

                lab_Msg.Text = "组盘成功！";
                gv_Bind();
                //txt_Tray_ID.Text = "";
                txt_Mat_Code.Text = "";
                txt_Mat_Num.Text = "";
                HD_SEC_MAT_ID.Value = "";
                //xgw 添加注释入库指令按钮（普通立体库不需要）
                //ibtn_RK.Visible = true;
            }
            else
            {
                lab_Msg.Text = strResMsg.ToString();
            }
        }
        catch (Exception ex)
        {
            lab_Msg.Text = ex.ToString();
        }

    }
    /// <summary>
    /// 仓库选择（注释，不是基本的功能）
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    //protected void rbtnl_Zone_SelectedIndexChanged(object sender, EventArgs e)
    //{
    //    if (rbtnl_Zone.SelectedValue == "1")
    //    {
    //        td_t.Visible = true;
    //        td_h.Visible = false;
    //    }
    //    else
    //    {
    //        td_t.Visible = false;
    //        td_h.Visible = true;
    //    }
    //}

    //有关平库的入库指令(系统不需要)
    //protected void ibtn_RK_Click(object sender, ImageClickEventArgs e)
    //{
    //    string strTray_ID = "H" + ddl_Zone.SelectedValue.ToString() + ddl_Dou.SelectedValue.ToString();    
    //    string strPos_ID = Convert.ToString(Convert.ToInt32(ddl_Zone.SelectedValue.ToString()) + 20) + "00000000" + Convert.ToString(ddl_Dou.SelectedValue.ToString());

    //    strSQL = @"UPDATE TRelations SET POS_ID='" + strPos_ID + "' WHERE TRAY_ID='" + strTray_ID + "'";
    //    cmd = dbHelper.GetSqlStringCommond(strSQL);
    //    dbHelper.ExecuteNonQuery(cmd);

    //    strSQL = @"UPDATE TBase_Position SET POS_STATE=1 WHERE POS_ID='" + strPos_ID + "'";
    //    cmd = dbHelper.GetSqlStringCommond(strSQL);
    //    dbHelper.ExecuteNonQuery(cmd);

    //    strSQL = @"INSERT INTO TCommands_HZK(SBH,DH,STATUS) VALUES('" + Convert.ToInt32(ddl_Zone.SelectedValue.ToString()) + "','" + Convert.ToInt32(ddl_Dou.SelectedValue.ToString()) + "',0)";
    //    cmd = dbHelper.GetSqlStringCommond(strSQL);
    //    dbHelper.ExecuteNonQuery(cmd);
    //    ibtn_RK.Visible = false;
    //}


    protected string CheckCode(string strTray_ID)
    {
        string strCode = "";

        strSQL = @"SELECT TBI.S01 FROM TBase_Storage TBS
	                    LEFT JOIN TRel_TrayPosContainer TRT ON TRT.ContainerID=TBS.ContainerID
	                    LEFT JOIN TBase_TrayPos TBT ON TBT.Relations_ID=TRT.Relations_ID
	                    LEFT JOIN TBase_Interface TBI ON TBI.Sec_Mat_ID=TBS.Sec_Mat_ID
	                    WHERE TBT.Tray_ID='" + strTray_ID + "'";
        cmd = dbHelper.GetSqlStringCommond(strSQL);

        DataSet ds = dbHelper.ExecuteDataSet(cmd);
        if (ds != null && ds.Tables[0].Rows.Count > 0)
        {
            strCode = ds.Tables[0].Rows[0][0].ToString();
        }
        return strCode;
    }
}
